local extension = Package("tuntian_cards", Package.CardPack)
extension.extensionName = "tuntian_studio"

local U = require "packages/utility/utility"
local TT = require "packages/tuntian_studio/utility"
local RUtil = require 'packages/diy_utility/_base' --@ 使用Ralph科技

Fk:loadTranslationTable{
  ["tuntian_cards"] = "屯田扩展牌",
}

local premeditate_rule = fk.CreateTriggerSkill{
  name = "#tuntian_premeditate_rule",
  events = {fk.EventPhaseStart},
  mute = true,
  global = true,
  priority = 0,
  can_trigger = function(self, event, target, player, data)
    return target == player and player.phase == Player.Judge
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:getCardIds(Player.Judge)
    for i = #cards, 1, -1 do
      if table.contains(player:getCardIds(Player.Judge), cards[i]) then
        if player.dead then return end
        local xumou = player:getVirualEquip(cards[i])
        if xumou and xumou.name == "tuntian__premeditate" then
          local use = U.askForUseRealCard(room, player, {cards[i]}, ".", "tuntian__premeditate",
            "#tuntian__premeditate-use:::"..Fk:getCardById(cards[i], true):toLogString(),
            {expand_pile = {cards[i]}, extra_use = true}, true, true)
          if use then
            room:setPlayerMark(player, "tuntian__premeditate_"..use.card.trueName.."-phase", 1)
            use.extra_data = use.extra_data or {}
            use.extra_data.premeditate = true
            room:useCard(use)
          else
            break
          end
        end
      end
    end
    cards = player:getCardIds(Player.Judge)
    local xumou = table.filter(cards, function(id)
      local card = player:getVirualEquip(id)
      return card and card.name == "tuntian__premeditate"
    end)
    room:moveCardTo(xumou, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "tuntian__premeditate", nil, true, player.id)
  end,
}
local premeditate_prohibit = fk.CreateProhibitSkill{
  name = "#tuntian_premeditate_prohibit",
  global = true,
  prohibit_use = function(self, player, card)
    return card and player:getMark("tuntian__premeditate_"..card.trueName.."-phase") > 0
  end,
}
Fk:addSkill(premeditate_rule)
Fk:addSkill(premeditate_prohibit)
Fk:loadTranslationTable{
  [ "#tuntian_premeditate_rule"] = "蓄谋",
  ["#tuntian_premeditate_prohibit"] = "蓄谋",
  ["#tuntian__premeditate-use"] = "你可以使用此蓄谋牌%arg，或点“取消”将所有蓄谋牌置入弃牌堆",
}

local tuntian_iceDamage = fk.CreateTriggerSkill {
  name = "#tuntian_iceDamage",
  mute = true,
  global = true,
  priority = 0.001,
  events = {fk.DamageCaused},
  can_trigger = function (self, event, target, player, data)
    if target ~= player then return false end
    if data.damageType == fk.IceDamage and data.skillName then
      return data.skillName ~= "ice__slash_skill" and not data.to:isNude()
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#IceDamage:"..data.to.id.."::"..data.damage)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = data.to
    for i = 1, 2 do
      if player.dead or to.dead or to:isNude() then break end
      local card = room:askForCardChosen(player, to, "he", self.name)
      room:throwCard({card}, data.card and data.card.extra_data and data.card.extra_data.change_reason or self.name, to, player)
    end
    return true
  end
}
Fk:addSkill(tuntian_iceDamage)
Fk:loadTranslationTable {
  ["#tuntian_iceDamage"] = "冰冻伤害",
  ["#IceDamage"] = "你是否想要防止对 %src 造成的%arg点冰冻伤害，依次弃置其两张牌？",
}

local present_skill = fk.CreateActiveSkill{
  name = "tuntian__present_skill&",
  prompt = "#tuntian__present_skill&",
  anim_type = "support",
  card_num = 1,
  target_num = 1,
  can_use = function(self, player)
    return table.find(player:getCardIds("h"), function(id) return Fk:getCardById(id):getMark("@@present") > 0 end)
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select):getMark("@@present") > 0 and table.contains(Self:getCardIds("h"), to_select)
  end,
  target_filter = function(self, to_select, selected, selected_cards, _, _, player)
    return #selected == 0 and to_select ~= player.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    U.presentCard(player, target, Fk:getCardById(effect.cards[1]))
  end,
}
Fk:addSkill(present_skill)

Fk:loadTranslationTable{
  ["tuntian__present_skill&"] = "赠予",
  [":tuntian__present_skill&"] = "出牌阶段，你可以从手牌中将一张有“赠”标记的牌正面向上赠予其他角色。若此牌不是装备牌，则进入该角色手牌区；若此牌是装备牌，"..
  "则进入该角色装备区且替换已有装备。",
  ["#tuntian__present_skill&"] = "将一张有“赠”标记的牌赠予其他角色",
  ["@@present"] = "<font color='yellow'>赠</font>",
}

local PresentCards = {}
local function addPreasentCard(c)
  extension:addCard(c)
  table.insert(PresentCards, c)
end

local frostmourne_skill = fk.CreateTriggerSkill{
  name = "#frostmourne_skill",
  attached_equip = "frostmourne",
  events = {fk.DamageCaused, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
    if event == fk.DamageCaused and target == player and data.card then
      return table.contains({"slash","death_coil"}, data.card.trueName) and data.damageType ~= fk.IceDamage
    elseif event == fk.AfterCardsMove then
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageCaused and target == player then
      return true
    elseif event == fk.AfterCardsMove then
      local cards = {}
      for _, move in ipairs(data) do
        if table.contains({self.name, "obliterate_skill"}, move.skillName) and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            table.insert(cards, info.cardId)
          end
        end
      end
      if #cards == 0 then return false end
      local ids, chosen = U.askforChooseCardsAndChoice(player, cards, {"tuntian__premeditate"}, self.name, "#frostmourne_skill-premeditate", {"Cancel"}, 1, #cards)
      if chosen == "Cancel" then return false end
      self.cost_data = ids
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.DamageCaused and target == player then
      TT.changeDamageType(data, fk.IceDamage, self.name)
    elseif event == fk.AfterCardsMove then
      local ids = self.cost_data
      for _, id in ipairs(ids) do
        --RUtil.premeditate(player, id, self.name, player.id)
        TT.premeditate(player, id, self.name, player.id)
      end
    end
  end,
}
Fk:addSkill(frostmourne_skill)
TT.loadSkill(frostmourne_skill, {name = "霜之哀伤"})

local frostmourne = fk.CreateWeapon{
  name = "&frostmourne",
  suit = Card.Spade,
  number = 5,
  attack_range = 3,
  equip_skill = frostmourne_skill,
}
extension:addCard(frostmourne)

local iceDamage_detial = TT.hrefString("冰冻伤害", "iceDamage_detial")
Fk:loadTranslationTable{
  ["iceDamage_detial"] = "伤害来源对一名角色造成冰冻伤害时，可以防止此伤害，依次弃置其两张牌。",
  ["#frostmourne_skill-premeditate"] = "霜之哀伤：可以用其中任意张牌蓄谋",
  ["tuntian__premeditate"] = "蓄谋",
}

TT.loadCard(frostmourne, {name = "霜之哀伤", des = {
  type = "装备牌·武器",
  attack_range = "3",
  weapon_effect = "你使用【杀】或【凋零缠绕】造成的伤害均视为"..iceDamage_detial.."，且你可以用以此法或【湮灭】弃置的牌蓄谋。"
}})

local army_of_the_dead_skill = fk.CreateActiveSkill {
  name = "army_of_the_dead_skill",
  prompt = "#army_of_the_dead_skill",
  can_use = Util.CanUse,
  target_num = 1,
  mod_target_filter = function(self, to_select, selected, player, card)
    return player.id ~= to_select
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(self, room, cardEffectEvent)
    local player = room:getPlayerById(cardEffectEvent.from)
    local to = room:getPlayerById(cardEffectEvent.to)
    local cards = {}
    for _ = 1, 5, 1 do
      if #room.discard_pile == 0 then break end
      local id = table.remove(room.discard_pile, #room.discard_pile)
      table.insert(cards, id)
    end
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, self.name, nil, true, player.id)
    for _, id in ipairs(cards) do
      local card = Fk:getCardById(id)
      if card.trueName == "slash" and not player:prohibitUse(card) and not player:isProhibited(to, card) and to:isAlive() then
        card.skillName = self.name
        room:useCard({
          card = card,
          from = player.id,
          tos = { {to.id} },
          extraUse = true,
        })
      end
    end
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name, nil, true, player.id)
  end,
}
TT.loadSkill(army_of_the_dead_skill, {name = "亡者大军"})
Fk:loadTranslationTable{ ["#army_of_the_dead_skill"] = "选择一名其他角色，你亮出最后进入弃牌堆的五张牌，对目标角色依次使用其中的【杀】" }

local army_of_the_dead = fk.CreateTrickCard{
  name = "&army_of_the_dead",
  skill = army_of_the_dead_skill,
  suit = Card.Spade,
  number = 6,
}
extension:addCard(army_of_the_dead)
TT.loadCard(army_of_the_dead, {name = "亡者大军", des = { type = "锦囊牌", event = "出牌阶段", target = "一名其他角色", effect = "亮出最后进入弃牌堆的五张牌，对目标角色依次使用其中的【杀】。" } })

local doom_pact_skill = fk.CreateActiveSkill {
  name = "doom_pact_skill",
  prompt = "#doom_pact_skill",
  can_use = Util.GlobalCanUse,
  on_use = function (self, room, cardUseEvent)
    local player = room:getPlayerById(cardUseEvent.from)
    if not cardUseEvent.tos or #TargetGroup:getRealTargets(cardUseEvent.tos) == 0 then
      cardUseEvent.tos = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if not room:getPlayerById(cardUseEvent.from):isProhibited(p, cardUseEvent.card) then
          TargetGroup:pushTargets(cardUseEvent.tos, p.id)
        end
      end
    end
    room.logic:getCurrentEvent():addExitFunc(
      function ()
        local death_knight_list = { "frostmourne", "anti_magic_shell", "army_of_the_dead", "death_and_decay", "death_coil", "death_grip", "doom_pact", "obliterate" }
        local death_knights = table.filter(Fk:getAllCardIds(), function (id)
          return table.contains(death_knight_list, Fk:getCardById(id).name)
        end)
        room:moveCardTo(death_knights, Card.Void, nil, fk.ReasonJustMove, self.name, nil, true, player.id)
      end
    )
  end,
  on_effect = function (self, room, cardEffectEvent)
    local to = room:getPlayerById(cardEffectEvent.to)
    room:throwCard(to:getCardIds("he"), self.name, to, to)
  end,
}
TT.loadSkill(doom_pact_skill, {name = "末日契约"})
Fk:loadTranslationTable{ ["#doom_pact_skill"] = "所有角色弃置所有牌。结算完成后，移除所有死亡骑士牌。" }

local doom_pact = fk.CreateTrickCard{
  name = "&doom_pact",
  skill = doom_pact_skill,
  suit = Card.Spade,
  number = 5,
}
extension:addCard(doom_pact)

TT.loadCard(doom_pact, {name = "末日契约", des = { type = "锦囊牌", event = "出牌阶段", target = "所有角色", effect = "目标角色弃置所有牌。结算完成后，移除所有死亡骑士牌。" } })

local death_grip_skill = fk.CreateActiveSkill {
  name = "death_grip_skill",
  prompt = "#death_grip_skill",
  can_use = Util.CanUse,
  target_num = 1,
  mod_target_filter = function(self, to_select, selected, player, card)
    return player.id ~= to_select and not Fk:currentRoom():getPlayerById(to_select):isKongcheng()
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(self, room, cardEffectEvent)
    local player = room:getPlayerById(cardEffectEvent.from)
    local to = room:getPlayerById(cardEffectEvent.to)
    if to:isKongcheng() then return end
    local cards = to:getCardIds("h")
    local availableCards = table.filter(cards, function(id)
      local card = Fk:getCardById(id)
      return U.getDefaultTargets(player, card, true, false)
    end)
    local use, _ = U.askforChooseCardsAndChoice(player, availableCards, {"OK"}, self.name, "#death_grip_skill-choose", {"Cancel"}, 1, 1, cards)
    if #use > 0 then
      local id = use[1]
      local card = Fk:getCardById(id)
      if not player.dead and U.getDefaultTargets(player, card, true, false) then
        U.askForUseRealCard(room, player, {id}, ".", self.name, "#death_grip_skill-use:::"..card:toLogString(), { expand_pile = {id} }, false, false)
      end
    end
  end,
}
TT.loadSkill(death_grip_skill, {name = "死亡之握"})
Fk:loadTranslationTable{
  ["#death_grip_skill"] = "选择一名其他角色，你观看其手牌并可以使用其中一张牌。",
  ["#death_grip_skill-choose"] = "死亡之握：可以使用其中一张",
  ["#death_grip_skill-use"] = "死亡之握：请使用%arg"
}

local death_grip = fk.CreateTrickCard{
  name = "&death_grip",
  skill = death_grip_skill,
  suit = Card.Spade,
  number = 2,
}
extension:addCard(death_grip)

TT.loadCard(death_grip, {name = "死亡之握", des = { type = "锦囊牌", event = "出牌阶段", target = "一名其他角色", effect = "观看目标角色的手牌并可以使用其中一张牌。" } })

local death_coil_skill = fk.CreateActiveSkill {
  name = "death_coil_skill",
  prompt = "#death_coil_skill",
  can_use = Util.CanUse,
  target_num = 1,
  mod_target_filter = function(self, to_select, selected, from, card)
    local to = Fk:currentRoom():getPlayerById(to_select)
    if from == to then
      return from:isWounded()
    else
      return from:compareDistance(to, 1, "<=")
    end
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(self, room, cardEffectEvent)
    local player = room:getPlayerById(cardEffectEvent.from)
    local to = room:getPlayerById(cardEffectEvent.to)
    if to.dead then return end
    if player == to then
      if player:isWounded() then
        TT.recover(to, 1, self.name)
      end
    else
      TT.damage(player, to, 1, self.name, cardEffectEvent.card)
    end
  end,
}
TT.loadSkill(death_coil_skill, {name = "凋零缠绕"})
Fk:loadTranslationTable{
  ["#death_coil_skill"] = "选择一名你至其距离1以内的角色，对其造成1点伤害。若目标角色为自己，则改为回复1点体力。",
}

local death_coil = fk.CreateTrickCard{
  name = "&death_coil",
  skill = death_coil_skill,
  suit = Card.Spade,
  number = 2,
}
extension:addCard(death_coil)

TT.loadCard(death_coil, {name = "凋零缠绕", des = { type = "锦囊牌", event = "出牌阶段", target = "一名与你距离1以内角色", effect = "对目标角色造成1点伤害。若目标角色为自己，则改为回复1点体力。" } })

local obliterate_skill = fk.CreateActiveSkill {
  name = "obliterate_skill",
  prompt = "#obliterate_skill",
  can_use = Util.CanUse,
  target_num = 1,
  mod_target_filter = function(self, to_select, selected, player, card)
    return player.id ~= to_select and not Fk:currentRoom():getPlayerById(to_select):isNude()
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(self, room, cardEffectEvent)
    local player = room:getPlayerById(cardEffectEvent.from)
    local to = room:getPlayerById(cardEffectEvent.to)
    local choices = {"1"}
    if #player:getCardIds("he") >= 3 then table.insert(choices, "3") end
    local n = tonumber(room:askForChoice(player, choices, self.name))
    local op_cards = room:askForCardsChosen(player, to, n, n, "he", self.name, "#obliterate_skill-choose:::"..n)
    room:throwCard(op_cards, self.name, to, player)
    if #op_cards == 3 then
      if player.dead then return end
      room:loseHp(player, 1, self.name)
    end
  end,
}
TT.loadSkill(obliterate_skill, {name = "湮灭"})
Fk:loadTranslationTable{
  ["#obliterate_skill"] = "选择一名其他角色，弃置其一或三张牌。若为三张，你失去1点体力。",
  ["#obliterate_skill-choose"] = "湮灭：请选择%arg张牌弃置",
}

local obliterate = fk.CreateTrickCard{
  name = "&obliterate",
  skill = obliterate_skill,
  suit = Card.Spade,
  number = 2,
}
extension:addCard(obliterate)

TT.loadCard(obliterate, {name = "湮灭", des = { type = "锦囊牌", event = "出牌阶段", target = "一名其他角色", effect = "弃置目标角色的一或三张牌。若为三张，你失去1点体力。" } })

local anti_magic_shell_skill = fk.CreateActiveSkill {
  name = "anti_magic_shell_skill",
  prompt = "#anti_magic_shell_skill",
  can_use = Util.CanUse,
  min_target_num = 1,
  max_target_num = 3,
  mod_target_filter = function(self, to_select, selected, user, card)
    local target = Fk:currentRoom():getPlayerById(to_select)
    if #selected == 0 then
      return true
    else
      local right_guys = table.map(selected, function(id) return Fk:currentRoom():getPlayerById(id):getNextAlive(false, 1, false) end)
      return table.contains(selected, target:getNextAlive(false, 1, false).id) or table.contains(right_guys, target)
    end
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(self, room, cardEffectEvent)
    local to = room:getPlayerById(cardEffectEvent.to)
    TT.EnterHidden(to)
  end,
}
TT.loadSkill(anti_magic_shell_skill, {name = "反魔法护罩"})
Fk:loadTranslationTable{
  ["#anti_magic_shell_skill"] = "选择至多三名座次连续的角色，令其隐匿。",
}

local anti_magic_shell = fk.CreateTrickCard{
  name = "&anti_magic_shell",
  skill = anti_magic_shell_skill,
  suit = Card.Spade,
  number = 4,
}
extension:addCard(anti_magic_shell)

TT.loadCard(anti_magic_shell, {name = "反魔法护罩", des = { type = "锦囊牌", event = "出牌阶段", target = "至多三名座次连续的角色", effect = "令目标角色隐匿。" } })

local death_and_decay_skill = fk.CreateActiveSkill {
  name = "death_and_decay_skill",
  prompt = "#death_and_decay_skill",
  can_use = Util.GlobalCanUse,
  on_use = Util.GlobalOnUse,
  mod_target_filter = Util.TrueFunc,
  target_filter = Util.FalseFunc,
  on_effect = function(self, room, cardEffectEvent)
    local to = room:getPlayerById(cardEffectEvent.to)
    if to.dead then return end
    room:loseHp(to, 1, self.name)
  end,
}
TT.loadSkill(death_and_decay_skill, {name = "死亡凋零"})
Fk:loadTranslationTable{
  ["#death_and_decay_skill"] = "令所有角色各失去1点体力。",
}

local death_and_decay = fk.CreateTrickCard{
  name = "&death_and_decay",
  skill = death_and_decay_skill,
  suit = Card.Spade,
  number = 3,
  multiple_targets = true,
}
extension:addCard(death_and_decay)

TT.loadCard(death_and_decay, {name = "死亡凋零", des = { type = "锦囊牌", event = "出牌阶段", target = "所有角色", effect = "目标角色失去1点体力。" } })

-- 兵临城下
local enemyAtTheGatesSkill = fk.CreateActiveSkill{
  name = "tuntian__enemy_at_the_gates_skill",
  target_num = 1,
  target_filter = function(self, to_select, selected, _, _, _, player)
    return #selected == 0 and player.id ~= to_select
  end,
  on_effect = function(self, room, cardEffectEvent)
    local player = room:getPlayerById(cardEffectEvent.from)
    local to = room:getPlayerById(cardEffectEvent.to)
    local cards = {}
    for i = 1, 4, 1 do
      local id = room:getNCards(1)[1]
      table.insert(cards, id)
      room:moveCardTo(id, Card.Processing, nil, fk.ReasonJustMove, self.name)
      local card = Fk:getCardById(id)
      if card.trueName == "slash" and not player:prohibitUse(card) and not player:isProhibited(to, card) and to:isAlive() then
        room:useCard({
          card = card,
          from = player.id,
          tos = { {to.id} },
          skillName = self.name,
          extraUse = true,
        })
      end
    end
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    room.logic:trigger(TT.ReviewToDiscardPile, player, {cards = cards})
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name, nil, true, player.id)
  end,
}

local enemyAtTheGates = fk.CreateTrickCard{
  name = "&tuntian__enemy_at_the_gates",
  suit = Card.Spade,
  number = 7,
  skill = enemyAtTheGatesSkill,
  is_damage_card = true,
}
extension:addCard(enemyAtTheGates)
TT.loadCard(enemyAtTheGates, { name = "兵临城下", des = { type = "锦囊牌", event = "出牌阶段", target = "一名其他角色", effect = "你依次展示牌堆顶四张牌，若为【杀】，你对目标使用之；若不为【杀】，将此牌置入弃牌堆。" } })

local shadeSkill = fk.CreateActiveSkill{
  name = "tuntian__shade_skill",
  can_use = Util.FalseFunc,
}
local shade = fk.CreateBasicCard{
  name = "&tuntian__shade",
  suit = Card.Spade,
  number = 1,
  skill = shadeSkill,
}
TT.loadCard(shade, { name = "影", des = { type = "基本牌", effect = "没有效果，不能被使用。<br/>当【影】进入弃牌堆后移出游戏。" } })
extension:addCard(shade)

local premeditate = fk.CreateDelayedTrickCard{
  name = "&tuntian__premeditate",
}
extension:addCard(premeditate)
Fk:loadTranslationTable{
  ["tuntian__premeditate"] = "蓄谋",
  [":tuntian__premeditate"] = "这张牌视为延时锦囊<br/><b>效果：</b>判定阶段开始时，按置入顺序（后置入的先处理）依次处理“蓄谋”牌：1.使用此牌，"..
  "然后此阶段不能再使用此牌名的牌；2.将所有“蓄谋”牌置入弃牌堆。",
}

-- 美人计
local honeyTrapSkill = fk.CreateActiveSkill{
  name = "&tuntian__honey_trap_skill",
  can_use = Util.CanUse,
  target_num = 1,
  mod_target_filter = function(self, to_select, selected, user, card, distance_limited)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return to_select ~= Self.id and not target:isKongcheng() and target:isMale()
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.to)
    local female = table.filter(room:getAlivePlayers(), function(p) return p:isFemale() end)
    if #female > 0 then
      for _, p in ipairs(female) do
        if target:isKongcheng() or target.dead then break end
        local id = room:askForCardChosen(p, target, "h", self.name)
        room:obtainCard(p, id, false, fk.ReasonPrey)
        if not player.dead and not p:isKongcheng() then
          local card = room:askForCard(p, 1, 1, false, self.name, false, ".", "#tuntian__honey_trap-card:"..player.id)
          room:obtainCard(player, card[1], false, fk.ReasonGive)
        end
      end
    end
    local from, to = player, target
    if player:getHandcardNum() == target:getHandcardNum() then
      return
    elseif player:getHandcardNum() > target:getHandcardNum() then
      from, to = target, player
    end
    room:damage({
      from = from,
      to = to,
      card = effect.card,
      damage = 1,
      skillName = self.name
    })
  end,
}

local honeyTrap = fk.CreateTrickCard{
  name = "&tuntian__honey_trap",
  skill = honeyTrapSkill,
  is_damage_card = true,
}
extension:addCard(honeyTrap)

Fk:loadTranslationTable{
  ["tuntian__honey_trap"] = "美人计",
  ["tuntian__honey_trap_skill"] = "美人计",
  [":tuntian__honey_trap"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一名有手牌的其他男性角色<br/><b>效果</b>：所有女性角色获得目标角色的一张手牌"..
  "并交给你一张手牌，然后你与目标中手牌数少的角色对手牌数多的角色造成1点伤害。",
  ["#tuntian__honey_trap-card"] = "美人计：请将一张手牌交给 %src",
}

-- 折戟
local broken_halberd = fk.CreateWeapon{
  name = "&tuntian__broken_halberd",
  suit = Card.Club,
  number = 1,
  attack_range = 0,
}
addPreasentCard(broken_halberd)

Fk:loadTranslationTable{
  ["tuntian__broken_halberd"] = "折戟",
  [":tuntian__broken_halberd"] = "装备牌·武器<br/><b>攻击范围</b>：0<br/>这是一把坏掉的武器……",
}

-- 引蜂衣
local beeClothSkill = fk.CreateTriggerSkill{
  name = "#tuntian__bee_cloth_skill",
  attached_equip = "bee_cloth",
  anim_type = "negative",
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted, fk.PreHpLost},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.DamageInflicted then
        return data.card and data.card.type == Card.TypeTrick
      elseif event == fk.PreHpLost then
        return data.skillName == "es__poison"
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
      data.damage = data.damage + 1
    elseif event == fk.PreHpLost then
      data.num = data.num + 1
    end
  end,
}
Fk:addSkill(beeClothSkill)

local bee_cloth = fk.CreateArmor{
  name = "&tuntian__bee_cloth",
  suit = Card.Club,
  number = 3,
  equip_skill = beeClothSkill,
}
addPreasentCard(bee_cloth)

Fk:loadTranslationTable{
  ["tuntian__bee_cloth"] = "引蜂衣",
  ["#tuntian__bee_cloth_skill"] = "引蜂衣",
  [":tuntian__bee_cloth"] = "装备牌·防具<br/><b>防具技能</b>：锁定技，你受到锦囊牌的伤害+1，因【毒】的效果失去体力+1。",
}

-- 锦盒
local brocadeBoxSkill = fk.CreateActiveSkill {
  name = "tuntian__brocade_box_skill",
  prompt = "#tuntian__brocade_box_skill",
  mute = true,
  anim_type = "negative",
  expand_pile = "$tuntian__present",
  attached_equip = "tuntian__brocade_box",
  card_num = 1,
  card_filter = function (self, to_select, selected, player)
    return #selected == 0 and table.contains(player:getPile("$tuntian__present"), to_select)
  end,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = effect.cards
    local box = table.find(player:getCardIds("e"), function (id) return Fk:getCardById(id).name == "tuntian__brocade_box" end)
    if box then table.insertIfNeed(cards, box) end
    for _, cid in ipairs(player:getCardIds("h")) do
      if Fk:getCardById(cid).suit == Fk:getCardById(cards[1]).suit then
        table.insertIfNeed(cards, cid)
      end
    end
    if #cards == 0 then return end
    room:throwCard(cards, self.name, player, player)
  end,
}
Fk:addSkill(brocadeBoxSkill)

local brocadeBoxTrigger = fk.CreateTriggerSkill {
  name = "tuntian__brocade_box_trigger",
  mute = true,
  global = true,
  events = {fk.AfterCardsMove},
  can_trigger = function (self, event, target, player, data)
    for index, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if Fk:getCardById(info.cardId).name == "tuntian__brocade_box" then
          self.cost_data = {index, move, info, info.cardId}
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local index, move, info, card = table.unpack(self.cost_data)
    if move.moveReason ~= fk.ReasonExchange then -- 特判交换
      if move.toArea == Card.PlayerEquip and player.id == move.to then -- 进装备区，塞牌
        local cards = room:getNCards(2)
        local present, _ = U.askforChooseCardsAndChoice(player, cards, {"OK"}, self.name, "#tuntian__brocade_box-toPresent", nil, 1, 1)
        room:setTag("tuntian__brocade_box:::"..card, present[1])
        player:addToPile("$tuntian__present", present, false, brocadeBoxSkill.name)
      elseif info.fromArea == Card.PlayerEquip and player.id == move.from then -- 出装备区，爆牌
        if move.toArea == Card.DiscardPile and move.skillName ~= brocadeBoxSkill.name then
          local hand_cards = player:getCardIds("h")
          if #hand_cards > 0 then
            room:throwCard(hand_cards, brocadeBoxSkill.name, player, player)
          end
        end
        local present = room:getTag("tuntian__brocade_box:::"..card)
        if present then
          room:setTag("tuntian__brocade_box:::"..card, nil)
          room:throwCard({present}, brocadeBoxSkill.name, player, player)
        end
      end
    else
      if info.fromArea == Card.Processing and move.toArea == Card.PlayerEquip and player.id == move.to then -- 交换，进装备区
        local present = room:getTag("tuntian__brocade_box:::"..card)
        if present then
          player:addToPile("$tuntian__present", present, false, brocadeBoxSkill.name)
        end
      end
    end
  end,
}
Fk:addSkill(brocadeBoxTrigger)

local brocade_box = fk.CreateTreasure {
  name = "&tuntian__brocade_box",
  suit = Card.Club,
  number = 10,
  equip_skill = brocadeBoxSkill,
}
addPreasentCard(brocade_box)

Fk:loadTranslationTable{
  ["tuntian__brocade_box"] = "锦盒",
  [":tuntian__brocade_box"] = "装备牌·宝物<br/><b>宝物技能</b>：当【锦盒】进入你的装备区时，你观看牌堆顶的两张牌并将其中一张扣于【锦盒】下，称为“礼”；"
  .."出牌阶段，你可以将“礼”置入弃牌堆。若如此做，你同时弃置所有与此“礼”花色相同的手牌以及【锦盒】；当【锦盒】以除此以外的方式进入弃牌堆时，你弃置所有手牌。",

  ["tuntian__brocade_box_skill"] = "锦盒",
  ["#tuntian__brocade_box_skill"] = "你是否想要发动“锦盒”，弃置“礼”、与“礼”花色相同的手牌，以及【锦盒】？",
  ["$tuntian__present"] = "礼",

  ["tuntian__brocade_box_trigger"] = "锦盒",
  ["#tuntian__brocade_box-toPresent"] = "锦盒：请选择一张牌扣于此牌下，称为“礼”",
}

return extension
